package com.gukun.springboot.tdengine.util;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class DownloadController {

    public static void main (String[] args) throws IOException {
        // 模拟的对象数据
        String data1 = "This is the content of file1.txt";
        String data2 = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}";
        String data3 = "Another file content for file3.txt";

        // 创建临时字节数组输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try (ZipOutputStream zos = new ZipOutputStream(baos)) {
            // 添加第一个文件
            addFileToZip(zos, "file1.txt", data1);
            // 添加第二个文件
            addFileToZip(zos, "data.json", data2);
            // 添加第三个文件
            addFileToZip(zos, "file3.txt", data3);
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileOutputStream fos = new FileOutputStream("D:\\iii.zip");
        fos.write(baos.toByteArray());
        fos.close();

    }

    /**
     * 将数据添加到 ZIP 输出流
     *
     * @param zos    ZIP 输出流
     * @param fileName 文件名
     * @param data    文件内容
     * @throws IOException 如果发生 I/O 错误
     */
    private static void addFileToZip(ZipOutputStream zos, String fileName, String data) throws IOException {
        // 创建 ZIP 条目
        ZipEntry zipEntry = new ZipEntry(fileName);
        zos.putNextEntry(zipEntry);

        // 将数据写入 ZIP 输出流
        zos.write(data.getBytes());
        zos.closeEntry();
    }
}